﻿using CoreShop.App.Units.Interface;
using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using Loading;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class GsGetAiCodeList : UIForm
    {
        public AiCodeGetInfoListModel SelectCurrentAiCodeInfo;
        OpaqueCommand cmd = new OpaqueCommand();
        public List<AiCodeGetInfoListModel> ListAIInfo ;
        public int aiType = 0;

        public GsGetAiCodeList(int _aiType)
        {
            aiType = _aiType;
            InitializeComponent();
        }

        public async void LoadDataAsync()
        {
            cmd.ShowOpaqueLayer(pnl_Form, 125, true);
            try
            {
                this.dgvAiInfoList.AutoGenerateColumns = false;
                //string result = new PosRemoteServices(AppConstVars.sysUser.ccessToken).GetActivateCodeList(AppConstVars.sysUser).MessageOrString;
                var resultModel = await SystemApiUtil.GetActivateCodeList(AppConstVars.sysUser);
                string result = resultModel.MessageOrString;
                GetAiCodeInfoListModel AiListInfo = result.JsonToEntity<GetAiCodeInfoListModel>();
                if (AiListInfo != null && AiListInfo.rows != null && AiListInfo.rows.Count > 0)
                {
                    ListAIInfo = AiListInfo.rows.FindAll(p => p.status == 0 && p.branchId == AppConstVars.sysStore.branchId.ToString() && (p.aiType == aiType));
                    this.dgvAiInfoList.DataSource = ListAIInfo;
                    this.dgvAiInfoList.Focus();
                }
            }
            catch { }
            finally
            {
                cmd.HideOpaqueLayer();
            }
        }

        private void GsGetAiCodeList_Load(object sender, EventArgs e)
        {
            LoadDataAsync();
        }

        private void dgvAiInfoList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvAiInfoList.Rows.Count >= 1)
            {
                btnSelect_Click(null, null);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (dgvAiInfoList.SelectedRows.Count == 1)
            {
                string aiId = dgvAiInfoList.SelectedRows[0].Cells["aiId"].Value.ToString();
                SelectCurrentAiCodeInfo = ListAIInfo.Find(item =>
                {
                    return (item.aiId == aiId);
                });
                if (SelectCurrentAiCodeInfo != null)
                    this.DialogResult = DialogResult.Yes;
            }
        }

        private void ubtn_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GsGetAiCodeList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                if (dgvAiInfoList.Rows.Count >= 1)
                {
                    btnSelect_Click(null, null);
                }
            }
        }

        private void btnSelect_Click_1(object sender, EventArgs e)
        {
            btnSelect_Click(null, null);
        }
    }
}
